package com.bdqn.zhang.servlet;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.zhang.dao.VivodingdanDao;
import com.bdqn.zhang.pojo.*;
import com.bdqn.zhang.service.ServiceVivodingdan;
import com.bdqn.zhang.service.ServiceVivoej;
import com.bdqn.zhang.service.ServiceVivoproduct;
import com.bdqn.zhang.service.impl.ServiceVivodingdanimpl;
import com.bdqn.zhang.service.impl.ServiceVivoejimpl;
import com.bdqn.zhang.service.impl.ServiceVivoproductimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "servletspcx", urlPatterns = {"/spcx"})
//文件上传
@MultipartConfig
public class Servletspcx extends HttpServlet {
    ServiceVivoproduct serviceVivoproduct = new ServiceVivoproductimpl();
    ServiceVivoej serviceVivoej = new ServiceVivoejimpl();
    ServiceVivodingdan serviceVivodingdan=new ServiceVivodingdanimpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理post传参乱码
        req.setCharacterEncoding("UTF-8");
        //处理响应乱码
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //opr 判断不同的业务类型
        String opr = req.getParameter("opr");
        //equals判断常量在前 不会出现空指针异常
        boolean flag;
        if ("getAll".equals(opr)) {
            //获取当前页
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            //全查
            List<Vivoproduct> all = serviceVivoproduct.getAll((int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
//            //获取表中所以的记录数
            int count = serviceVivoproduct.getCount();
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("retrieve".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            int name = 0;
            if (p_name != null && !" ".equals(p_name)) {
                if ("上架状态".equals(p_name)) {
                    name = 1;
                } else if ("下架状态".equals(p_name)) {
                    name = 0;
                } else if ("预售状态".equals(p_name)) {
                    name = -1;
                }
                List<Vivoproduct> mh = serviceVivoproduct.retrieveztcx(name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceVivoproduct.retrievemhztcx(name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("del".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = serviceVivoproduct.del(ids[j]);        //循环执行删除
                serviceVivoej.del(ids[j]);
            }
            flag = i > 0;
            resp.getWriter().write(String.valueOf(flag));


            // String id = req.getParameter("id");

        }else if ("delsc".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = serviceVivoproduct.delsc(ids[j]);        //循环执行删除
                serviceVivoej.delsc(ids[j]);
            }
            flag = false;
            if (i > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        }
        else if ("huifu".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = serviceVivoproduct.delhuifu(ids[j]);        //循环执行删除
                serviceVivoej.delhuifu(ids[j]);
            }
            flag = i > 0;
            resp.getWriter().write(String.valueOf(flag));


            // String id = req.getParameter("id");

        }
        else if ("leix".equals(opr)) {
            String name = "一级分类";
            List<Vivoproduct> all = serviceVivoproduct.getAllsss(name);
            String datajson = JSONObject.toJSONString(all);
            resp.getWriter().write(datajson);
        } else if ("leixbj".equals(opr)) {
            String name = "二级分类";
            String id = req.getParameter("id");
            int rr = Integer.parseInt(id);
            List<Vivoproduct> all = serviceVivoproduct.getAllys(name, rr);
            String datajson = JSONObject.toJSONString(all);
            resp.getWriter().write(datajson);
        } else if ("yijxg".equals(opr)) {
            String id = req.getParameter("id");
            List<Vivoej> all = serviceVivoej.yjxg(id);
            String datajson = JSONObject.toJSONString(all);
            resp.getWriter().write(datajson);
        } else if ("yijxgs".equals(opr)) {
            String id = req.getParameter("id");
            List<Vivoej> all = serviceVivoej.yjxgs(id);
            String datajson = JSONObject.toJSONString(all);
            resp.getWriter().write(datajson);
        } else if ("spleix".equals(opr)) {
            String name = "二级分类";
            String rr = req.getParameter("rr");
            req.getSession().setAttribute("rr", rr);
            List<Vivoproduct> all = serviceVivoproduct.getAlls(name, rr);
            String datajson = JSONObject.toJSONString(all);
            resp.getWriter().write(datajson);
        } else if ("ztxg".equals(opr)) {
            String id = req.getParameter("id");
            String zhi = req.getParameter("zhi");
            int zhis = Integer.parseInt(zhi);
            int zt = 0;
            if (zhis == 0) {
                zt = 1;
            } else if (zhis == 1) {
                zt = -1;
            } else if (zhis == -1) {
                zt = 0;
            }
            int count = serviceVivoproduct.gai(new Vivoproduct(zt, id));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = count > 0;
            resp.getWriter().write(String.valueOf(flag));
        } else if ("ztcx".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            int name = 0;
            if (p_name != null && !" ".equals(p_name)) {
                if ("上架状态".equals(p_name)) {
                    name = 1;
                } else if ("下架状态".equals(p_name)) {
                    name = 0;
                } else if ("预售状态".equals(p_name)) {
                    name = -1;
                }
                List<Vivoproduct> mh = serviceVivoproduct.ztcx(name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceVivoproduct.mhztcx(name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("addSave2".equals(opr)) {
            String id = req.getParameter("id");
            String sj = req.getParameter("sp");
            String miaosu = req.getParameter("miaosu");
            String images_str = req.getParameter("images_str");
            String images_strs = req.getParameter("images_strs");
            String zp = req.getParameter("photo");
            String jg = req.getParameter("jg");
            String kcs = req.getParameter("kc");
            String statuss = req.getParameter("status");
            String photo = "static/img/pbimg/" + zp;
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String dates = df.format(date);
            int kc = Integer.parseInt(kcs);
            Double jgs = Double.parseDouble(jg);
            int pv = 0;
            int status =Integer.parseInt(statuss);
            int shanchu=1;
            int count = serviceVivoproduct.edits(new Vivoproduct(miaosu, jgs, pv, status, dates, kc, sj, photo, images_str, images_strs,shanchu, id));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        }else if ("bg".equals(opr)) {
            String p_name = req.getParameter("p_name");
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            String rr = req.getParameter("rr");
            LayData laydata = new LayData();
            if ("仅查询商品名称".equals(rr)) {
                List<Vivoproduct> mh = serviceVivoproduct.yhmcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceVivoproduct.spmccx(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }  else if ("仅查询商品介绍".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceVivoproduct.spjscx(p_name));
                laydata.setData(serviceVivoproduct.xbcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }else if ("仅查询商品价格".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceVivoproduct.spjgcx(p_name));
                laydata.setData(serviceVivoproduct.spjg(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }else if ("仅查询商品上架时间".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceVivoproduct.spdate(p_name));
                laydata.setData(serviceVivoproduct.spsjsj(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }
        }else if ("retrievebg".equals(opr)) {
            String p_name = req.getParameter("p_name");
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            String rr = req.getParameter("rr");
            LayData laydata = new LayData();
            if ("仅查询商品名称".equals(rr)) {
                List<Vivoproduct> mh = serviceVivoproduct.retrieveyhmcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceVivoproduct.retrievespmccx(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }  else if ("仅查询商品介绍".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceVivoproduct.retrievespjscx(p_name));
                laydata.setData(serviceVivoproduct.retrievexbcx(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }else if ("仅查询商品价格".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceVivoproduct.retrievespjgcx(p_name));
                laydata.setData(serviceVivoproduct.retrievespjg(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }else if ("仅查询商品上架时间".equals(rr)) {
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(serviceVivoproduct.retrievespdate(p_name));
                laydata.setData(serviceVivoproduct.retrievespsjsj(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit)));
                String jsonString = JSONObject.toJSONString(laydata);
                resp.getWriter().write(jsonString);
            }
        }
        else if ("upload".equals(opr)) {
            String realPath = req.getSession().getServletContext().getRealPath("static");
            Part part = req.getPart("file");
            String fileName = part.getSubmittedFileName();
            AjaxObject object = new AjaxObject();
            object.setImgSrc(fileName);
            resp.getWriter().write(JSONObject.toJSONString(object));
        } else if ("addSave".equals(opr)) {
            String name = req.getParameter("name");
            String names = req.getParameter("rr");
            String ss = req.getParameter("ss");
            String images_str = req.getParameter("images_str");
            String images_strs = req.getParameter("images_strs");
            String password = req.getParameter("password");
            String emails = req.getParameter("email");
            String phones = req.getParameter("phone");
            String zp = req.getParameter("photo");
            int phone = Integer.parseInt(phones);
            Double email = Double.parseDouble(emails);
            Date date = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String dates = df.format(date);
            // Formats a Date into a date/time string.// 2019-07-03
            String photo = "static/img/pbimg/" + zp;
            String leix = "商品";
            names = names.replace("'", "");
            ss = ss.replace("'", "");
            int pv = 0;
            int status = 1;
            int shanchu=1;
            int count = serviceVivoej.add(new Vivoej(leix, name, ss, photo, status,shanchu));//匿名对象 调用完之后会被内存回收 省内存空间
            serviceVivoproduct.add(new Vivoproduct(name, password, email, pv, status, dates, phone, images_str, images_strs,shanchu));
            flag = false;
            if (count > 0) {
                flag = true;
            }
            resp.getWriter().write(String.valueOf(flag));
        } else if ("sjlx".equals(opr)) {
            //全查
            List<Vivoej> all = serviceVivoej.getAllr();
//            //获取表中所以的记录数
            int count = all.size();
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("delej".equals(opr)) {
            int i = 0;
            String id = req.getParameter("id");
            String[] ids = id.split(",");  //分割id，成一个字符串数组
            int n = ids.length;
            for (int j = 0; j < n; j++) {
                i = serviceVivoproduct.del(ids[j]);        //循环执行删除
                serviceVivoej.del(ids[j]);
            }
            flag = i > 0;
            resp.getWriter().write(String.valueOf(flag));
        } else if ("jbsc".equals(opr)) {
            String id = req.getParameter("id");
            //分割id，成一个字符串数组
            int n = serviceVivoej.cz(id);       //循环执行删除
            flag = false;
            if (n > 0) {
                flag = true;
            } else {
                serviceVivoej.delss(id);
            }
            resp.getWriter().write(String.valueOf(flag));
        } else if ("addSave3".equals(opr)) {
            String ids = req.getParameter("eid");
            String sj = req.getParameter("leix");
            String zp = req.getParameter("photo");
            String photo = "static/img/pbimg/" + zp;
            int id = Integer.parseInt(ids);
            int count = serviceVivoej.edits(new Vivoej(id, sj, photo));//匿名对象 调用完之后会被内存回收 省内存空间
            flag = count > 0;
            resp.getWriter().write(String.valueOf(flag));
        } else if ("addSaveej".equals(opr)) {
            String rr = req.getParameter("rr");
            String ss = req.getParameter("ss");
            String name = req.getParameter("name");
            String zp = req.getParameter("photo");
            String sex = req.getParameter("sex");
            String photo = "static/img/pbimg/" + zp;
            rr = rr.replace("'", "");
            ss = ss.replace("'", "");
            int zhuangtai;
            if ("禁用".equals(sex)) {
                zhuangtai = 0;
            } else {
                zhuangtai = 1;
            }
            int shanchu=1;
            String leix;
            if ("无".equals(rr) && "无".equals(ss)) {
                leix = "一级分类";
                int count = serviceVivoej.updatyij(new Vivoej(name, leix, rr, photo, zhuangtai,shanchu));//匿名对象 调用完之后会被内存回收 省内存空间
                flag = count > 0;
                resp.getWriter().write(String.valueOf(flag));
            } else if (!"无".equals(rr) && "无".equals(ss)) {
                leix = "二级分类";
                int count = serviceVivoej.updatej(new Vivoej(name, leix, rr, photo, zhuangtai,shanchu));//匿名对象 调用完之后会被内存回收 省内存空间
                flag = count > 0;
                resp.getWriter().write(String.valueOf(flag));
            } else if (!"无".equals(rr) && !"无".equals(ss)) {
                leix = "商品";
               int sc=1;
                int count = serviceVivoej.updatejsan(new Vivoej(name, leix, ss, photo, zhuangtai,shanchu));
                serviceVivoproduct.zt(new Vivoproduct(name, zhuangtai,sc));
                flag = count > 0;
                resp.getWriter().write(String.valueOf(flag));

            }
        } else if ("getAll2".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            if (p_name != null && !" ".equals(p_name)) {
                List<Vivoproduct> mh = serviceVivoproduct.dan(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceVivoproduct.mhdan(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        }else if ("retrieve2".equals(opr)) {
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            LayData laydata = new LayData();
            String p_name = req.getParameter("p_name");
            if (p_name != null && !" ".equals(p_name)) {
                List<Vivoproduct> mh = serviceVivoproduct.retrieve2dan(p_name, (int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
                int count = serviceVivoproduct.retrieve2mhdan(p_name);
                laydata.setCode(0);
                laydata.setMsg("");
                laydata.setCount(count);
                laydata.setData(mh);
            }
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        }
        else if ("indexsp".equals(opr)) {
            int count = serviceVivoproduct.getCount();
            String jsonString = JSONObject.toJSONString(count);
            resp.getWriter().write(jsonString);
        }else if ("dingdancx".equals(opr)) {
            //获取当前页
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            //全查
            List<Vivodingdan> all = serviceVivodingdan.getAll((int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
//            //获取表中所以的记录数
            int count = serviceVivodingdan.getCount();
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        } else if ("hsz".equals(opr)) {
            //获取当前页
            int int_current = Integer.parseInt(req.getParameter("page"));
            String limit = req.getParameter("limit");
            //全查
            List<Vivoproduct> all = serviceVivoproduct.getAllhsz((int_current - 1) * Integer.parseInt(limit), Integer.parseInt(limit));
//            //获取表中所以的记录数
            int count = serviceVivoproduct.getCounthsz();
            LayData laydata = new LayData();
            laydata.setCode(0);
            laydata.setMsg("");
            laydata.setCount(count);
            laydata.setData(all);
            String jsonString = JSONObject.toJSONString(laydata);
            resp.getWriter().write(jsonString);
        }
    }
}
